# 内存检查相关的编译选项

function(XCMAKE_TRY_ADD_MEM_CHECK_BUILD_OPTION target_name)
  message(STATUS "try add memcheck build option for static target: " ${target_name})
  if (UNIX)
    target_compile_options(${target_name} PRIVATE -fsanitize=address,undefined)
    target_link_options(${target_name} PRIVATE -fsanitize=address,undefined)
  elseif (MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /analyze:leakcheck /analyze:uninitialized /W4")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /INCREMENTAL:NO")
  else()
    message(FATAL_ERROR "Memory check is not supported on this platform")
  endif()
endfunction()

function(XCMAKE_TRY_ADD_MEM_CHECK_BUILD_OPTION_FOR_STATIC target_name)
  message(STATUS "try add memcheck build option for static target: " ${target_name})
  if (UNIX)
    target_compile_options(${target_name} PRIVATE -fsanitize=address,undefined)
  elseif (MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /analyze:leakcheck /analyze:uninitialized /W4")
  else()
    message(FATAL_ERROR "Memory check is not supported on this platform")
  endif()
endfunction()
